﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for TinTucDA
/// </summary>
public class TinTucDA
{
    Connection con = new Connection();
	public TinTucDA()
	{
		
	}
    public DataTable getAll()
    {
        try
        {
            string str = "select tintuc.* from TinTuc  order by id DESC";
            SqlCommand com = new SqlCommand(str,con.getCon());
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {
            
            throw;
        }

    }
    public DataTable getAll(int loaitin,int theloai)
    {
        try
        {
            string str = "select tintuc.* from TinTuc,LoaiTin where tintuc.loaitin=loaitin.id and loaitin.theloai=@theloai and loaitin.id=@loaitin order by tintuc.id DESC";
            SqlCommand com = new SqlCommand(str, con.getCon());
            com.Parameters.AddWithValue("@theloai", theloai);
            com.Parameters.AddWithValue("@loaitin", loaitin);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            throw;
        }

    }
    public DataTable getByID(int id)
    {
        try
        {
            string str = "select * from TinTuc where ID=@ID";
            SqlCommand com = new SqlCommand(str, con.getCon());
            com.Parameters.Add("@ID", SqlDbType.Int).Value = id;
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            throw;
        }
    }
    public void updateImage(string type,string name)
    {
        try
        {
            Connection conn = new Connection();
            string str = "update TinTuc set HinhAnh='~/Image/TinTuc/" + name + "." + type + "' where ID=" + getMaxID();
            SqlCommand com = new SqlCommand(str, conn.getCon());
            conn.openCon();
            com.ExecuteNonQuery();
            conn.closeCon();
        }
        catch (Exception)
        {

            throw;
        }

    }
    public void updateImagebyID(string type, string name,int id)
    {
        try
        {
            Connection conn = new Connection();
            string str = "update TinTuc set HinhAnh='~/Image/TinTuc/" + name + "." + type + "' where ID=" + id;
            SqlCommand com = new SqlCommand(str, conn.getCon());
            conn.openCon();
            com.ExecuteNonQuery();
            conn.closeCon();
        }
        catch (Exception)
        {

            throw;
        }

    }
    public string getMaxID()
    {
        try
        {
            Connection conn = new Connection();
            string str = "select Max(ID) from TinTuc";
            SqlCommand com = new SqlCommand(str, conn.getCon());
            conn.openCon();
            SqlDataReader dr = com.ExecuteReader();
            if (dr.Read())
            {
                return dr[0].ToString();
            }
            conn.closeCon();
            return "";
        }
        catch (Exception)
        {

            throw;
        }
    }
}